package com.litorg.demo.db.config;

import org.opentsdb.client.OpenTSDBClient;
import org.opentsdb.client.bean.request.Point;
import org.opentsdb.client.bean.request.Query;
import org.opentsdb.client.bean.request.SubQuery;
import org.opentsdb.client.bean.response.QueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * spring openTSDB服务
 *
 * @author cw
 **/
@Component
@ConditionalOnProperty(prefix = "assembly-switch", name = "opentsdb", havingValue = "true")
public class OpenTSDBService {
    private static final Logger log = LoggerFactory.getLogger(OpenTSDBService.class);
    @Autowired
    public OpenTSDBClient openTSDBClient;

    /**
     * 新增数据
     *
     * @param metric    metric
     * @param tags      tags
     * @param value     value
     * @param timestamp timestamp
     * @return true/false
     */
    public boolean put(String metric, Map<String, String> tags, Number value, long timestamp) {
        try {
            Point point = Point.metric(metric)
                    .tag(tags)
                    .value(timestamp, value)
                    .build();
            openTSDBClient.put(point);
            return true;
        } catch (Exception e) {
            log.error("Open TSDB put data error, metric: {}, tags: {}, timestamp: {}, value: {}",
                    metric, tags.toString(), timestamp, value, e);
           return false;
        }
    }

    /**
     * 新增数据
     *
     * @param metric metric
     * @param tags   tags
     * @param value  value
     * @return true/false
     */
    public boolean put(String metric, Map<String, String> tags, Number value) {
        return put(metric, tags, value, System.currentTimeMillis());
    }

    /**
     * 查询数据
     *
     * @param metric     metric
     * @param tags       tags
     * @param begin      开始时间
     * @param end        结束时间
     * @param aggregator aggregator
     * @return 查询结果
     */
    public List<QueryResult> get(String metric, Map<String, String> tags, String begin, String end, SubQuery.Aggregator aggregator) {
        List<QueryResult> resultList = null;
        try {
            Query query = Query.begin(begin)
                    .end(end)
                    .sub(SubQuery.metric(metric)
                            .aggregator(aggregator)
                            .tag(tags)
                            // .downsample("1h-avg-zero")
                            .build())
                    .build();
            resultList = openTSDBClient.query(query);
        } catch (Exception e) {
            log.error("Open TSDB get data error, metric: {}, tags: {}, begin: {}, end: {}, aggregator: {}",
                    metric, tags.toString(), begin, end, aggregator.getName(), e);
        }
        return resultList;
    }
}